package com.yunshangsci.tio.websocket;

import org.tio.server.ServerTioConfig;
import org.tio.websocket.server.WsServerStarter;

public class WsStarter {
    private WsServerStarter wsServerStarter;
    private ServerTioConfig serverTioConfig;

    /**
     *
     * @author tanyaowu
     */
    public WsStarter(int port, WsMsgHandler wsMsgHandler) throws Exception {
        wsServerStarter = new WsServerStarter(port, wsMsgHandler);

        serverTioConfig = wsServerStarter.getServerTioConfig();
        serverTioConfig.setName(WsServerConfig.PROTOCOL_NAME);
//        serverTioConfig.setServerAioListener(new WsServerAioListener());

        //设置ip监控
//        serverTioConfig.setIpStatListener(new WsIpStatListener());
        //设置ip统计时间段
//        serverTioConfig.ipStats.addDurations(WsServerConfig.IpStatDuration.IPSTAT_DURATIONS);

        //设置心跳超时时间
        serverTioConfig.setHeartbeatTimeout(WsServerConfig.HEARTBEAT_TIMEOUT);

    }

    /**
     * @author tanyaowu
     */
    public static void start() throws Exception {
        WsStarter appStarter = new WsStarter(WsServerConfig.SERVER_PORT, new WsMsgHandler());
        appStarter.wsServerStarter.start();
    }

    /**
     * @return the serverTioConfig
     */
    public ServerTioConfig getServerTioConfig() {
        return serverTioConfig;
    }

    public WsServerStarter getWsServerStarter() {
        return wsServerStarter;
    }

    public static void main(String[] args) throws Exception {
        //启动websocket server
        start();
    }

}
